<?php
class SmsController extends ApiController
{
    public $login_require = array(); 
    public $self_error_list = array(
        'user_exist' => array('errno' => 1130001, 'errmsg' => '用户已存在,请登录'),
        'user_not_exist' => array('errno' => 1130002, 'errmsg' => '用户不存在,请先注册'),
        'api_error' => array('errno' => 1130003, 'errmsg' => '请求接口异常,请重试'),
        'type_error' => array('errno' => 1130004, 'errmsg' => '类型不存在,请重试'),
        );
    public function actionSend()
    {
        $extend='';
        $result=array();
        $sms_param=array();
        $result['error']=0;
        $extra = array();
        $mobile = Yii::app()->request->getPost('mobile');
        $type = Yii::app()->request->getPost('type', 0);

        if (!empty($mobile)) {
            $is_find_by_self =false;
            $is_find =false;
            if (empty($type) || !in_array($type, array(1,2)) ) {
                $this->status = $this->error_list['type_error'];
                return;
            }
              
            $user_exist = User::model()->checkMobile($mobile);
            if ($type == 2 ) {
                if ($user_exist) {
                    $this->status = $this->error_list['user_exist'];
                    return;
                } 
                $sms_template_code='SMS_86540065';//TODO...
            } else if ($type == 1) { 
                if (!$user_exist) {
                    $this->status = $this->error_list['user_not_exist'];
                    return;
                }
                $sms_template_code='SMS_86650054';//TODO...
            } 
            $captcha = mt_rand(1000, 9999);
            Yii::app()->cache->set('captcha_' . $mobile, $captcha, 10 * 60);
            $sms_param['code'] = ''.$captcha.'';
            $code = array();
            $code['code'] = $captcha;
            $content ='短信验证码:'.$captcha.'(10分钟内有效)';
            $this->status = $this->error_list['success'];
            if (YII_DEBUG && false) {
                $this->result['captcha'] = $captcha;
                return;
            } else { 
                //$return_data = WxUtil::aliSend($sms_param,$mobile,$sms_template_code,$extend);
                //$return_data = SMS::send($content, $mobile);
                $return_data = WxUtil::aliyunSend($mobile,$sms_template_code,$code,$extra);//阿里云通信  短信发送接口
                //记录日志
                $log = array();
                $log['code'] = $code;
                $log['sms_param'] = $sms_param;
                $log['rec_num'] = $mobile;
                $log['sms_template_code'] = $sms_template_code;
                $log['extend'] = $extend;
                $log['result'] = $return_data;
                $log['is_api'] = 1;
                file_put_contents(Yii::app()->getRuntimePath() . '/send_sms_' . date("Y_W") . '.txt', sprintf("%s %s %s", date("Y-m-d H:i:s"), json_encode($log), PHP_EOL), FILE_APPEND);
            }
        } else {
            $this->status = $this->error_list['error'];
        }
    }
    
}
?>
